<template>
  <div>
    <el-form>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" ref="productTable" style="width: 100%" border>
      <el-table-column prop="id" header-align="center" align="center" width="80" label="ID"></el-table-column>
      <el-table-column label="商品图片" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.productImage" width="40" height="40" class="head_pic" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="300" align="center" prop="productName"></el-table-column>
      <el-table-column label="商品sku名称" width="150" align="center" prop="productSkuName"></el-table-column>
      <el-table-column prop="profit" label="每周收益" width="100" align="center"></el-table-column>
      <el-table-column prop="productSkuId" label="商品skuID" width="100" align="center"></el-table-column>
      <el-table-column prop="totalPrice" label="商品总价格" width="100" align="center"></el-table-column>
      <el-table-column label="操作" width="90" align="center">
        <template slot-scope="scope">
          <p>
            <el-button size="mini" type="text" @click="addOrUpdateHandle(scope.row.id)">详情</el-button>
          </p>
        </template>
      </el-table-column>
    </el-table>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>
<script>
import AddOrUpdate from "./inve-add-update";
export default {
  data() {
    return {
      dataForm: {
        id: 0,
        installment: 0,
        productImage: "",
        productName: "",
        productSkuName: 0,
        profit: 0
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    };
  },
  activated() {
    this.getDataList();
  },
  components: {
    AddOrUpdate
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/productInvestment/investmentList"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          productName: this.dataForm.productName,
          productImage: this.data
        })
      }).then(({ data }) => {
        if (data && data.status === 200) {
          this.dataList = data.data.list;
          this.totalPage = data.data.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.userId;
          });
      this.$confirm(
        `确定对[id=${userIds.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          this.$http({
            // url: this.$http.adornUrl("/sys/user/delete"),
            method: "post",
            url: this.$http.adornUrl("/productInvestment/save/" + id),
            method: "get",
            data: this.$http.adornData(userIds, false)
          }).then(({ data }) => {
            if (data && data.status === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(data.message);
            }
          });
        })
        .catch(() => {});
    },
    shangjia() {}
  }
};
</script>
